﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmartProject.Entity;
using SmartProject.Exceptions;

using NHibernate;

namespace SmartProject.BusinessRules
{
    public class UsernameSimple : BusinessRule
    {
        public override string Name
        {
            get { return "Simple username"; }
        }

        public override string Description
        {
            get 
            {
                return "Username cannot be less than 5 characters and it can consist of any characters.";
            }
        }

        public override void Implement(ISession session, EntityBase entity)
        {
            if (entity.GetOperation() == EntityBase.Operations.Save)
            {
                string userName = ((User)entity).Username;
                if (userName.Length < 4)
                {
                    throw new Exceptions.BusinessRuleViolation("The username cannot be less than 5 characters long.");
                }
            }
        }

        public override bool QueryBooleanProperty(string propertyName)
        {
            switch (propertyName)
            {
                case "IsEnableUserNameTextBox":
                    return true;
            }

            throw new MissingPropertyName(String.Format(
                "Property name is missing: {0}", propertyName));
        }
    }
}
